package com.ykq.service.impl;

import com.ykq.dao.MenuDao;
import com.ykq.dao.UserDao;
import com.ykq.pojo.Menu;
import com.ykq.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: vue_axios_ssm
 * @description:
 * @author: 闫克起2
 * @create: 2023-05-18 11:34
 **/
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    public List<Menu> selectByUserid(Integer userid) {
        //当前用户的所有菜单
        List<Menu> menus = menuDao.selectByUserid(userid);

        //找出一级菜单
        List<Menu> firstMenus=new ArrayList<Menu>();
        for(Menu menu:menus){
             if(menu.getPid()==0){
                 firstMenus.add(menu);
             }
        }

        //查找一级菜单下对应的二级菜单
        for(Menu first:firstMenus){
             List<Menu> children=new ArrayList<Menu>();
             for(Menu menu:menus){
                  if(menu.getPid().equals(first.getId())){
                       children.add(menu);
                  }
             }
             first.setChildren(children);
        }

        return firstMenus;
    }
}
